package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.mapper.RepetitionMapper;
import com.example.demo.mapper.ScoreMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.entity.Message;
import com.example.demo.entity.Repetition;
import com.example.demo.entity.Student;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/repetition")
public class RepetitionController {
    @Autowired
    private  RepetitionMapper rm;
    @Autowired
    private  TeacherMapper tm;
    @Autowired
    private  StudentMapper  sm;
    @Autowired
    private  ScoreMapper scm;

    private static final Pattern pattern = Pattern.compile("\\d{4}-(0[6-8]|0[1-3])-\\d{2}");
    @RequestMapping("/apply")
    public void apply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String ids = request.getParameter("id");
        String name = request.getParameter("name");
        String college = request.getParameter("college");
        String major = request.getParameter("major");
        String term = request.getParameter("term");
        String detail = request.getParameter("detail");
        if(ids!=null&&!"".equals(ids)&&name!=null&&!"".equals(name)&&college!=null&&!"".equals(college)&&major!=null&&!"".equals(major)&&term!=null&&!"".equals(term)&&detail!=null&&!"".equals(detail)) {
            Matcher matcher = pattern.matcher(new Date(System.currentTimeMillis()).toString());
            if (matcher.matches()){
                int id = Integer.valueOf(ids);
            Random r = new Random();
            int random = r.nextInt(9000) + 1000;
            Repetition repetition = new Repetition(random, id, name, college, major, term, detail, new Date(System.currentTimeMillis()), "待审核");
            int add = rm.add(repetition);
            if (add > 0) {
                response.getWriter().write(JSONObject.toJSONString(Message.success("apply is success.Please wait pass")));
            } else
                response.getWriter().write(JSONObject.toJSONString(Message.failure("apply is error please repeat apply")));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("error in date is not allow,please in 1-3 or 6-8")));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("error in param is null")));
    }

    @RequestMapping("/check")
    public void check(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String college="";
        List<Repetition> repetitions=new ArrayList<>();
        List<Repetition> repetitionss=new ArrayList<>();
        User user = (User)request.getSession().getAttribute("user");
        if(user.getRole()==1){
            repetitions=rm.selectAll();
        }else if(user.getRole()==2){
            college = tm.findTeaByName(user.getName()).getCollege();
            repetitionss = rm.selectByCollege(college);
            for(Repetition r:repetitionss){
                if(sm.selectStuById(r.getId()).getGrade().equals(tm.findTeaByName(user.getName()).getGrade())){
                    repetitions.add(r);
                }
            }
        }
        response.getWriter().write(JSONObject.toJSONString(Message.success(repetitions)));
    }

    @RequestMapping("/detail")
    public void detail(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        int reid = Integer.valueOf(request.getParameter("reid"));
        Repetition repetition = rm.selectByReId(reid);
        response.getWriter().write(JSONObject.toJSONString(Message.success(repetition)));
    }

    @RequestMapping("/checksuccess")
    public void checksuccess(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        int reid = Integer.valueOf(request.getParameter("reid"));
        int id = Integer.valueOf(request.getParameter("id"));
        String term = request.getParameter("term");
        int update = rm.update("审核通过", reid);
        if(update>0) {
            response.getWriter().write(JSONObject.toJSONString(Message.success("check success")));
            Student student = sm.selectStuById(id);
            int grade = Integer.valueOf(student.getGrade().split("级")[0])+1;
            int before = Integer.valueOf(term.split("-")[0]);
            int after = Integer.valueOf(term.split("-")[1]);
            int ss = Integer.valueOf(term.split("-")[2]);
            int i = sm.updateStuGrade(grade + "级", id);
            if(i>0){
                if(ss==1){
                    scm.delete(id,term);
                }else{
                    String terms=before+"-"+after+"-1";
                    scm.delete(id,term);
                    scm.delete(id,term);
                }
            }
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("check failure")));
    }

    @RequestMapping("/checkfailure")
    public void checkfailure(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        int reid = Integer.valueOf(request.getParameter("reid"));
        int update = rm.update("审核不通过", reid);
        if(update>0)
            response.getWriter().write(JSONObject.toJSONString(Message.success("check success")));
        else response.getWriter().write(JSONObject.toJSONString(Message.failure("check failure")));
    }
}
